import urlConfig from './config.js'
// console.log(urlConfig)
const request = {}
const headers = {}
const PORT1 = '/baseinfo'

request.globalRequest = (url, method, data, power) => {
	let islogin = uni.getStorageSync('loginStatus').token;
	if(islogin){
		headers['token'] = islogin;
	}
	/*     权限判断 因为有的接口请求头可能需要添加的参数不一样，所以这里做了区分
	    1 == 不通过access_token校验的接口
	    2 == 文件下载接口列表
	    3 == 验证码登录 */
	
	// switch (power) {
	// 	case 1:
	// 		headers[''] = 'Basic a3N1ZGk6a3N1ZGk='
	// 		break;
	// 	case 2:
	// 		headers['Authorization'] = 'Basic a3N1ZGlfcGM6a3N1ZGlfcGM='
	// 		break;
	// 	case 3:
	// 		responseType = 'blob'
	// 		break;
	// 	default:
	// 		headers['Authorization'] = `Bearer ${
	// 			this.$store.getters.userInfo
	// 		}`
	// 		headers['TENANT-ID'] = this.$store.getters.userInfo.tenant_id
	// 		break;	
	// }
	return uni.request({
		url : urlConfig + url,
		method,
		data:data,
		dataType:'json',
		header:headers
	}).then(res => {
		uni.hideLoading();
		// console.log(res);
		
		// 成功返回数据
		if(res[1].data.code == 200 || res[1].data.code == 201){
			return res[1].data;
		}
		
		if(res[1].data.code == 403){
			uni.clearStorage();
			setTimeout(function() {
				uni.reLaunch({
					url:'/pages/login/login'
				});
			}, 1000);
			return false;
		}
		if(res[1].data.code == 402){
			uni.clearStorage();
			uni.showToast({
				title:res[1].data.errmsg,
				icon:'none'
			});
			setTimeout(function() {
				uni.reLaunch({
					url:'/pages/login/login'
				});
			}, 1000);
			return false;
		}
		
		
	}).catch(parmas => {
		switch (parmas.code) {
			case 401:
				uni.clearStorageSync()
				break;
			default:
				uni.showToast({
					title: parmas.message,
					icon: 'none'
				})
				return Promise.reject()
				break;
		}
	})
}

export default request
